home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / Dialoger / Dialoger.c next >
Encoding:
C/C++ Source or Header  |  1989-06-16  |  3.9 KB  |  179 lines  |  [TEXT/KAHL]

  1.  
  2. /*    This is a preprocessor to the _ModalDialog call
  3.     ModalDialog(filter:ProcPtr; VAR itemHit:integer):integer;
  4.     
  5.     It will check for a nil pointer, and if so replace it will a 
  6.     pointer to my routine.
  7.         
  8. */
  9. #include    <strings.h>
  10.  
  11. #define SETUPA4()    _A4SET(1)
  12. #define    INITA4()    _A4SET(0)
  13. #define    maxStrings    30
  14.  
  15. extern    char    *modalDTrap;
  16. Boolean    seenUpdate, hasEditFields;
  17. char    letterArray[maxStrings];    /*array of the first chars of the first maxStrings buttons*/
  18. ProcPtr    oldFilter;
  19.  
  20. /* ================================================================ */
  21. void DoAbout()
  22. /* ---------------------------------------------------------------- */
  23. {
  24. #define    textBase 10
  25.  
  26.     int    oldFont, oldSize, counter, myTicks;
  27.     GrafPtr    curPort;
  28.     Rect    theRect;
  29.     long    dummy, curTicks;
  30.     EventRecord    theEvent;
  31.     
  32.     GetPort(&curPort);
  33.     oldFont = curPort->txFont;
  34.     oldSize = curPort->txSize;
  35.     TextFont(1);    /* geneva */
  36.     TextSize(9);
  37.     
  38.     myTicks = 5;
  39.     for (counter=curPort->portRect.left;
  40.          counter<=curPort->portRect.right+10;
  41.          counter+=5)
  42.     {
  43.         SetRect(&theRect, curPort->portRect.left, 0, curPort->portRect.right+10,textBase+2);
  44.         EraseRect(&theRect);
  45.         MoveTo(counter, textBase);
  46.            DrawString("\pAlerter by Leonard Rosenthol");
  47.         curTicks = TickCount();
  48.         while (TickCount()<curTicks+myTicks);
  49.         if (EventAvail(keyDownMask+mDownMask, &theEvent)) { myTicks = 1; }    /* set it to like no delay */
  50.     }
  51.     TextFont(oldFont);
  52.     TextSize(oldSize);
  53. }
  54.  
  55. /* ================================================================ */
  56. pascal Boolean    callOldFilter (dPtr, theEvent, itemHit)
  57. register DialogPtr dPtr;
  58. register EventRecord * theEvent;
  59. register int   *itemHit;
  60. /* ---------------------------------------------------------------- */
  61. {
  62.     extern ProcPtr oldFilter;
  63.     
  64.     return(CallPascalB(dPtr, theEvent, itemHit, oldFilter));
  65. }
  66.  
  67. /* ================================================================ */
  68. pascal Boolean    alertFilter (dPtr, theEvent, itemHit)
  69. register DialogPtr dPtr;
  70. register EventRecord * theEvent;
  71. register int   *itemHit;
  72. /* ---------------------------------------------------------------- */
  73. {
  74.     register int    Key, counter = 1;
  75.     GrafPtr    oldPort;
  76.     Boolean    retVal, done = FALSE;
  77.  
  78.     asm {
  79.         movem.l    a1-a5/d0-d7,-(sp)    /* save the regs */
  80.         };
  81.     SETUPA4();
  82.         
  83.     retVal = FALSE;
  84.     if (seenUpdate)
  85.     {
  86.         hasEditFields = FALSE;    /* init it going into WalkList */
  87.         WalkList(dPtr);
  88.     }
  89.         
  90.     if (seenUpdate && isoptionkey())
  91.     {
  92.         GetPort(&oldPort);
  93.         SetPort(dPtr);
  94.         DoAbout();
  95.         SetPort(oldPort);
  96.         seenUpdate = FALSE;
  97.     }
  98.     
  99.     if (theEvent->what == updateEvt)
  100.         seenUpdate = TRUE;
  101.         
  102.     if (theEvent->what != keyDown)
  103.     {
  104.         if (oldFilter==0L)
  105.             return (false);
  106.         else
  107.             return(callOldFilter(dPtr, theEvent, itemHit));
  108.     }
  109.  
  110.     Key = (unsigned char) theEvent->message;
  111.  
  112.     if (((hasEditFields) && (theEvent->modifiers & cmdKey)) ||
  113.         (!hasEditFields))
  114.     {
  115.         do{
  116.             if ( (Key==letterArray[counter]) || (Key==letterArray[counter]-0x20))
  117.             {
  118.                 *itemHit = counter;
  119.                 FlashItem(dPtr, counter);
  120.                 retVal = TRUE;
  121.                 done = TRUE;
  122.             }
  123.             counter++;
  124.         } while ((!done) && (counter<maxStrings));
  125.     }
  126.     
  127.     if ((Key=='\r') || (Key=='\003'))
  128.     {
  129.         *itemHit = ((DialogPeek)dPtr)->aDefItem;    /*get the default item*/
  130.         FlashItem(dPtr, *itemHit);
  131.         retVal = TRUE;
  132.     }
  133.     
  134.     if (!retVal)    /* found nothing else to do */
  135.     {
  136.         if (oldFilter==0L)
  137.             return (false);
  138.         else
  139.             return(callOldFilter(dPtr, theEvent, itemHit));
  140.     }
  141.     
  142.     asm {
  143.         Movem.l    (sp)+,a1-a5/d0-d7    /* restore all regs */
  144.         };
  145.     return (retVal);
  146.     
  147. }
  148.  
  149.  
  150. pascal int    myModalDialog(theFilter, theID)
  151. int        *theID;
  152. ProcPtr    theFilter;
  153. {
  154.     asm {
  155.         movem.l    a1-a5/d0-d7,-(sp)    /* save the regs */
  156.         };
  157.  
  158.     SETUPA4();
  159.     seenUpdate = FALSE;
  160.  
  161.     if (theFilter==0L)
  162.     {
  163.         theFilter=(ProcPtr)alertFilter;
  164.         oldFilter = 0L;    /* set it to nil SPECIFICALLY */
  165.     }
  166.     else
  167.     {
  168.         oldFilter = (ProcPtr)theFilter;    /* set it to the old filter */
  169.         theFilter=(ProcPtr)alertFilter;
  170.     };
  171.     
  172.     asm {
  173.         move.l    modalDTrap,a0
  174.         Movem.l    (sp)+,a1-a5/d0-d7    /* restore all regs */
  175.         unlk    a6
  176.         jmp(a0)                        /* Do the load   */
  177.         };
  178. };
  179.